/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-29
 * Time: 19:52
 */
public class prune_Tree {
    public TreeNode pruneTree(TreeNode root) {
        purune(root);
        if (root.val == 0 && root.left == null && root.right == null) {
            root = null;
        }
        return root;
    }

    private int purune(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = purune(root.left);
        if (left == 0) {
            root.left = null;
        }
        int right = purune(root.right);
        if (right == 0) {
            root.right = null;
        }

        return root.val + left + right;
    }
}

